<template>
  <el-card  class="cate-container">
    <el-breadcrumb separator="/">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>文章管理</el-breadcrumb-item>
  <el-breadcrumb-item>文章分类</el-breadcrumb-item>
</el-breadcrumb>
  <el-row type="flex" justify="end">
    <el-col :span="2"><el-button type="primary" size="small" @click="AddDialogVisible = true">添加文章</el-button></el-col>
  </el-row>
  <el-table
    :data="cateList"
    style="width:100%"
    border=""
    stripe
    width="120">
    <el-table-column
      label="序号"
      type="index"
      width="120">
    </el-table-column>
    <el-table-column
      prop="cate_name"
      label="分类名称"
      >
    </el-table-column>
    <el-table-column
      prop="cate_alias"
      label="分类别名">
    </el-table-column>
     <el-table-column
      label="操作">
      <template #default="{row}">
        <!-- 在element-ui组件库的table组件中, 列如果想要访问数据,可以使用作用域插槽接收数据
            接收的数据都存在了row(table-column组件内部插槽上绑定的属性)的属性中
            row表示当前行的数据
         -->

        <el-button type="primary" size="mini" @click="showEditDialog(row.id)">修改</el-button>
          <span>&nbsp;&nbsp;&nbsp;</span>
        <el-dialog
  title="修改文章分类"
  :visible="editDialogVisible"
  width="35%"
  @close="editDialogVisible=false">
   <!-- 添加的表单 -->
    <el-form :model="editForm" :rules="addRules" ref="editRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
    </el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false" >取 消</el-button>
    <el-button type="primary" @click="editCate">确 定</el-button>
  </span>
</el-dialog>
         <el-button type="danger" size="mini" @click="removeCate(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 添加的对话框 -->
  <el-dialog
  title="添加文章分类"
  :visible="AddDialogVisible"
  width="35%"
  @close="resetAddForm">
   <!-- 添加的表单 -->
    <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
    </el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="AddDialogVisible=false" >取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
</el-dialog>
  </el-card >
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { reqAddArticleCate, reqDelArticle, reqGetCateInfoById, reqUpdataArticle } from '../../api/article'
export default {
  name: 'cateList',
  created () {
    // 获取文章分类
    this.getCateList()
  },
  data () {
    return {
      // 控制添加对话框组件
      // 添加表单的数据
      AddDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: ['blur', 'change'] },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
    // 添加的表单验证
  },

  computed: {
    // 映射文章分类列表
    ...mapState('article', ['cateList'])
  },
  methods: {
    ...mapActions('article', ['getCateList']),
    // 重置表单 对话框关闭完成后的事件
    resetAddForm () {
      // 1. 关闭对话框
      this.AddDialogVisible = false
      // 2. 重置表单数据和校验规则
      this.$refs.addRef.resetFields()
    },

    // 新增文章分类
    addCate () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const { data } = await reqAddArticleCate(this.addForm)
        // 提示用户
        if (data.code !== 0) return
        this.$message.success(data.message)
        // 关闭对话框
        this.AddDialogVisible = false
        // 重新获取文章分类
        this.getCateList()
      })
    },
    // 删除文章分类
    async removeCate (id) {
      // 判断要删除的是否为前两个分类
      if (id === 1 || id === 2) return this.$message.warning('管理员不允许删除前两个分类！')
      // 询问用户是否要删除
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 取消了删除
      if (confirmResult !== 'confirm') return this.$message.info('已取消操作')

      // console.log(confirmResult)
      // 调用接口方法
      const { data: res } = await reqDelArticle(id)
      // console.log(res)
      // 判断删除的结果
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 再次调用文章获取方法
      this.getCateList()
    },
    // 获取对话框内容
    async showEditDialog (id) {
      if (id === 1 || id === 2) return this.$message.warning('管理员不允许修改前两个分类！')
      // 开启对话框
      this.editDialogVisible = true
      // 发送请求
      const { data } = await reqGetCateInfoById(id)
      // 数据回显 将结果赋值给双向绑定给表单的editForm
      if (data.code !== 0) return this.$message.error('获取分类失败')
      this.editForm = data.data
      // console.log(res)
    },
    // 修改文章
    editCate () {
      this.$refs.editRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await reqUpdataArticle(this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭对话框
        this.editDialogVisible = false
        // 重新获取内容
        this.getCateList()
      })
    }
  }
}
</script>

<style>

</style>
